import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/welcome'
import users from '../components/user/users'
import rights from '../components/power/rights'
import roles from '../components/power/roles'
import Cate from '../components/goods/Cate'
import Cate_params from '../components/goods/Cate_params'
import List from '../components/goods/List'
import system from '../components/system/system'
import password from '../components/account/password'
import finance from '../components/finance/finance'
import member from '../components/member/memberList'
import equipment from '../components/equipment/equipment'
import epidemic from '../components/epidemic/epidemic'
import test from '../components/test'

//解决Elementur导仇。的vue-router在3.e版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location){
    return originalPush.call(this,location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
    {
        path: '/login', component: Login
    },
    {
        path: '/home', component: Home,
        redirect: '/welcome',
        children: [
            {path: '/welcome', component: Welcome},
            {path: '/users', component: users},
            {path: '/rights', component: rights},
            {path: '/roles', component: roles},
            {path: '/categories', component: Cate},
            {path: '/params', component: Cate_params},
            {path: '/goods', component: List},
            {path: '/sysconfig', component: system},
            {path: '/password', component: password},
            {path: '/finance', component: finance},
            {path: '/member', component: member},
            {path: '/equipment', component: equipment},
            {path: '/epidemic', component: epidemic},
            {path: '/reports', component: test},
        ]
    },
    {
        path: '/', redirect: '/login'
    }
]

const router = new VueRouter({
    routes
})
//路由导航守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    const tokenstr = window.sessionStorage.getItem('token')
    console.log(tokenstr)
    if (!tokenstr) return next('/login')
    next()
})


export default router
